﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obout.Grid;
using HostingDomainWebsite;
using Obout.Ajax.UI.HTMLEditor;

namespace Hosting
{
    public partial class About : System.Web.UI.Page
    {

        protected SiteMaster master;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["username"] == null)
            {
                Response.Redirect("AdminPage.aspx");
            }
            master = Master as SiteMaster;
            master.setCurrentNav("About");
            //load();

        }

        //protected void load()
        //{
        //    Hosting.Class.About about = new Class.About();
        //    var a = about.getAll("-1");
        //    gAbout.DataSource = a;
        //    gAbout.DataBind();
        //}

        #region [Insert new Category Title]
        protected void gAbout_InsertCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.About about = new Hosting.Class.About();
            var isValid = about.addValid(e.Record["ABTitle"].ToString());
            if (isValid == 1)
            {

                string active = "false";
                if (ABActive.Checked)
                {
                    active = "true";
                }

                var isInsert = about.add(e.Record["ABTitle"].ToString(), e.Record["ABContent"].ToString(), Boolean.Parse(active));
                if (isInsert == 0)
                {
                    master.getMessageSuccess("Insert a new Category Title successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when insert a new Category Title!");
                }
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Title is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Title is greater than 100 characters");
            }
        }
        #endregion

        #region [Update new Category Title]
        protected void gAbout_UpdateCommand(object sender, GridRecordEventArgs e)
        {
            var id = e.Record["ABId"].ToString();
            var no = e.Record["ABTitle"].ToString();
            Hosting.Class.About about = new Hosting.Class.About();
            var isValid = about.editValid(e.Record["ABId"].ToString(), e.Record["ABTitle"].ToString());
            if (isValid == 1)
            {
                string active = "false";
                if (ABActive.Checked)
                {
                    active = "true";
                }

                var isUpdate = about.edit(e.Record["ABId"].ToString(), e.Record["ABTitle"].ToString(), e.Record["ABContent"].ToString()
                    , Boolean.Parse(active));
                if (isUpdate == 0)
                {
                    master.getMessageSuccess("Update Category Title successfully!");
                }
                else
                {
                    master.getMessageError("There is an error when update Category Title!");
                }
            }
            else if (isValid == 2)
            {
                master.getMessageAttention("Id is null or empty");
            }
            else if (isValid == 3)
            {
                master.getMessageAttention("Id is not valid");
            }
            else if (isValid == 10)
            {
                master.getMessageAttention("Name is null or empty");
            }
            else if (isValid == 11)
            {
                master.getMessageAttention("Name is greater than 2000 characters");
            }
            else if (isValid == 20)
            {
                master.getMessageAttention("Description is greater than 2000 chareacters");
            }
        }
        #endregion

        #region [Delete new Category Title]
        protected void gAbout_DeleteCommand(object sender, GridRecordEventArgs e)
        {
            Hosting.Class.About about = new Hosting.Class.About();
            var isDelete = about.delete(e.Record["ABId"].ToString());
            if (isDelete != 0)
            {
                master.getMessageError("There is an error when delete an About!");
            }
            else if (isDelete == 0)
            {
                master.getMessageSuccess("Delete successfully!");
            }
        }
        #endregion

    }

}